package com.css.zfzx.sjcj.modules.hddcwyTrench.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyTrench.repository.HddcWyTrenchNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyTrench.repository.HddcWyTrenchRepository;
import com.css.zfzx.sjcj.modules.hddcwyTrench.repository.entity.HddcWyTrenchEntity;
import com.css.zfzx.sjcj.modules.hddcwyTrench.service.HddcWyTrenchService;
import com.css.zfzx.sjcj.modules.hddcwyTrench.viewobjects.HddcWyTrenchQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyTrench.viewobjects.HddcWyTrenchVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zyb
 * @date 2020-12-02
 */
@Service
public class HddcWyTrenchServiceImpl implements HddcWyTrenchService {

	@Autowired
    private HddcWyTrenchRepository hddcWyTrenchRepository;
    @Autowired
    private HddcWyTrenchNativeRepository hddcWyTrenchNativeRepository;

    @Override
    public JSONObject queryHddcWyTrenchs(HddcWyTrenchQueryParams queryParams , int curPage, int pageSize) {
        Page<HddcWyTrenchEntity> hddcWyTrenchPage = this.hddcWyTrenchNativeRepository.queryHddcWyTrenchs(queryParams
                , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyTrenchPage);
        return jsonObject;
    }


    @Override
    public HddcWyTrenchEntity getHddcWyTrench(String id) {
        HddcWyTrenchEntity hddcWyTrench = this.hddcWyTrenchRepository.findById(id).orElse(null);
         return hddcWyTrench;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyTrenchEntity saveHddcWyTrench(HddcWyTrenchEntity hddcWyTrench) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyTrench.setId(uuid);
        hddcWyTrench.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyTrench.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyTrenchRepository.save(hddcWyTrench);
        return hddcWyTrench;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyTrenchEntity updateHddcWyTrench(HddcWyTrenchEntity hddcWyTrench) {
        hddcWyTrench.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyTrench.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyTrenchRepository.save(hddcWyTrench);
        return hddcWyTrench;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyTrenchs(List<String> ids) {
        List<HddcWyTrenchEntity> hddcWyTrenchList = this.hddcWyTrenchRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyTrenchList) && hddcWyTrenchList.size() > 0) {
            for(HddcWyTrenchEntity hddcWyTrench : hddcWyTrenchList) {
                this.hddcWyTrenchRepository.delete(hddcWyTrench);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

}
